// 获取文章id
let id = new URLSearchParams(location.search).get('id')

function rander() {
    $.ajax({
        url: 'http://localhost:8888/api/articles/' + id + '/comments',
        success: function (res) {
            console.log(res)
            if (res.status === 0) {
                let arr = ['<h4><i class="sprites"></i>评论区</h4>']
                res.data.forEach((item) => {
                    arr.push(`<div class="kr_comment_card">
                    <div class="img-wrap">
                        <img src="./uploads/avatar_3.jpg" alt="">
                    </div>
                    <div class="info">
                        <p>${item.uname} · <span>${item.cdate.substr(0,10)}</span></p>
                        <p>${item.content}</p>
                    </div>
                    <a href="javascript:;" class="like">${item.count}</a>
                </div>`)
                })
                $('#comment-list').html(arr.join(''))
            }

        }
    })
}

rander()


// 发表评论
$('.form').on('submit', function (e) {
    e.preventDefault()
    let fd = $(this).serialize()
    $.ajax({
        type: 'post',
        url: 'http://localhost:8888/api/articles/' + id + '/comments',
        data: fd,
        success: function (res) {
            // 发表评论成功
            layer.msg(res.message)
            $('.form').get(0).reset()
            $('.word_count').text('0/1000');
            rander()
        }
    })

})

// 
$('#comment-content').on('input', function (res) {
    let length = $(this).val().length;
    $('.word_count').text(length + '/1000');
})